﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MainApp
{
    public partial class Product_new : DevExpress.XtraEditors.XtraUserControl, IEnumerable<Dispenser>
    {
        private Guid productid;
        private decimal maxValue;
        private string productName;

        private List<Dispenser> dispensers = new List<Dispenser>();
        private List<DevExpress.XtraLayout.LayoutControlItem> newLayOut = new List<DevExpress.XtraLayout.LayoutControlItem>();
        private Dictionary<Dispenser, DevExpress.XtraLayout.LayoutControlItem> mapping = new Dictionary<Dispenser, DevExpress.XtraLayout.LayoutControlItem>();

        private readonly Size defaultSize = new System.Drawing.Size(152, 93);
        private bool today, canEdit;

        #region Constructors
        public Product_new()
        {
            InitializeComponent();
        }

        public Product_new(Guid productid, string productName, decimal maxValue, bool today, bool canEdit, decimal price)
            : this()
        {
            this.productid = productid;
            this.maxValue = maxValue;
            this.productName = productName;
            groupControl.Text = productName;
            btnAddNew.Enabled = today && canEdit;
            if (btnAddNew.Enabled)
            {
                btnAddNew.Click += new EventHandler(btnAddNew_Click);
            }
            this.today = today;
            this.canEdit = canEdit;
            Price = price;
            spPrice.EditValueChanged += new EventHandler(spPrice_EditValueChanged);
        }

        #endregion

        public void AddDispenser(Guid id, string name, decimal oldValue = 0, decimal newValue = 0)
        {
            maxValue -= oldValue;
            Dispenser dispenser = new Dispenser(id, name, oldValue, newValue, today, canEdit);
            dispensers.Add(dispenser);
            dispenser.Name = "dispenser " + dispenser.ID;
            dispenser.ValueChanged += new EventHandler(dispenser_ValueChanged);
            dispenser.Deleted += new EventHandler(dispenser_Deleted);
            spTotal.Value += dispenser.TotalInput;
            spTotalMoney.Value += Price * dispenser.TotalInput;
            DevExpress.XtraLayout.LayoutControlItem layItem = layout.AddItem(dispenser.Name, dispenser);
            layItem.TextVisible = false;
            layItem.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            mapping.Add(dispenser, layItem);

            layItem.MaxSize = defaultSize;
            layItem.MinSize = defaultSize;
            layItem.Size = defaultSize;
            if (newLayOut.Count > 0)
            {
                layItem.Move(newLayOut[newLayOut.Count - 1], DevExpress.XtraLayout.Utils.InsertType.Bottom);
            }
            else
            {
                layItem.Move(layoutSample, DevExpress.XtraLayout.Utils.InsertType.Bottom);
            }
            newLayOut.Add(layItem);
        }

        private bool CheckValid()
        {
            //if (spNewValue.Value < minValue)
            //{
            //    lblWarning.Text = "Chỉ số mới không thể nhỏ hơn chỉ số cũ";
            //    return false;
            //}
            if (TotalInput > maxValue)
            {
                lblWarning.Text = "Vượt quá số xăng trong kho";
                lblWarning.Visible = true;
                return false;
            }
            lblWarning.Visible = false;
            return true;
        }
        #region Properties
        public bool IsValid { get { return CheckValid(); } }
        public Guid ProductID { get { return productid; } }
        public decimal TotalInput
        {
            get
            {
                //decimal tmp = 0;
                //foreach (Dispenser item in dispensers)
                //{
                //    tmp += item.TotalInput;
                //}
                //return tmp;
                return spTotal.Value;
            }
        }
        public decimal Price { get { return spPrice.Value; } set { spPrice.EditValue = value; } }
        #endregion

        #region Events
        public event EventHandler ValueChanged;
        void dispenser_ValueChanged(object sender, EventArgs e)
        {
            decimal tmp = 0;
            foreach (Dispenser item in dispensers)
            {
                tmp += item.TotalInput;
            }
            spTotal.EditValue = tmp;
            spTotalMoney.EditValue = tmp * Price;
            this.ValueChanged.Invoke(this, e);
        }

        public event EventHandler AddNewClicked;
        void btnAddNew_Click(object sender, EventArgs e)
        {
            this.AddNewClicked.Invoke(this, e);
        }

        public event EventHandler DispenserDeleted;
        void dispenser_Deleted(object sender, EventArgs e)
        {
            Dispenser dispenser = sender as Dispenser;
            int pos = -1;
            for (int i = 0; i < dispensers.Count; i++)
            {
                if (dispensers[i].ID == dispenser.ID)
                {
                    pos = i;
                }
            }
            if (pos >= 0)
            {

                //layout.HideItem(layout.Items.FindByName("dispenser " + dispenser.ID));

                mapping[dispensers[pos]].Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                spTotal.Value -= dispensers[pos].OldValue;
                this.DispenserDeleted.Invoke(dispensers[pos], e);
                dispensers.RemoveAt(pos);
                newLayOut.RemoveAt(pos);
            }
            else
            {
                throw new Exception("Cannot find dispenser to delete");
            }
        }

        //public event EventHandler NameOfDispenserChanged;

        void spPrice_EditValueChanged(object sender, EventArgs e)
        {
            spTotalMoney.EditValue = spTotal.Value * Price;            
            this.ValueChanged.Invoke(this, e);
        }
        #endregion

        public IEnumerator<Dispenser> GetEnumerator()
        {
            return dispensers.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}
